package com.zhou.framework.controller;

import com.zhou.framework.model.RS;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

/**
 * @author lang.zhou
 * @date 2022/6/17 14:38
 */
@RestController
@RequestMapping("/error")
public class ErrorController {
    private static List<String> userAgentStr = Arrays.asList("firefox", "applewebkit", "chromeframe", "chrome", "msie", "rv:11");

    public boolean isBrowser(HttpServletRequest request){
        String userAgent = request.getHeader("User-Agent").toLowerCase();
        boolean isNeedCheck = false;
        for (String item : userAgentStr) {
            if (userAgent.contains(item)) {
                isNeedCheck = true;
                break;
            }
        }
        return isNeedCheck;
    }
    @RequestMapping("/404")
    public Object page404(HttpServletRequest request){
        if(isBrowser(request)){
            return str("页面找不到","页面找不到",404);
        }else{
            return RS.failed("Not found",404);
        }

    }
    @RequestMapping("/500")
    public Object page500(HttpServletRequest request){
        if(isBrowser(request)){
            return str("发生错误","服务器发生错误",500);
        }else{
            return RS.failed("Error occur",500);
        }
    }
    @RequestMapping("/400")
    public Object page400(HttpServletRequest request){
        if(isBrowser(request)){
            return str("请求不合法","请求不合法","REQUEST");
        }else{
            return RS.failed("Error request",400);
        }
    }
    public String str(String title,String str,Object code){
        return "<!DOCTYPE HTML>\n" +
                "<html>\n" +
                "<head>\n" +
                "    <title>"+title+"</title>\n" +
                "    <meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\"/>\n" +
                "</head>\n" +
                "<body style=\"background: #d2f5f1;\">\n" +
                "<div>\n" +
                "<h1 style=\"color:red;text-align:center;font-size:60px;text-shadow: 2px 2px 5px black;\">ERROR "+code+"</h1>" +
                "<h1 style=\"color:gray;text-align:center;font-size:40px;text-shadow: 2px 2px 2px black;\">"+str+"</h1>" +
                "<span style=\"position:fixed;width:100%;bottom:45px;left:0;color:gray;text-align:center;\">QQ：2250884056</span>" +
                "<span style=\"position:fixed;width:100%;bottom:20px;left:0;color:gray;text-align:center;\">微信小程序：zk表情头像库</span>" +
                "</div>\n" +
                "</body>\n" +
                "</html>";
    }
}
